﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using TJADRICD.Model;
using TJADRICD.DLL;
namespace TJADRICD.CollaborativeDesignPlugin.Form
{
    public partial class SignAnother : System.Windows.Forms.Form
    {
        private AuthorizeDao authorizeDao;
        private UserRoleDao userRoleDao;
        private AccountDao accountDao;
        //不用授权码时的可选对象
        private List<String> userRoleNoCodeList;

        //用授权码时的可选对象
        private List<String> userRoleCodeList;

        private String signId;
        public String SignId
        {
            get { return signId; }
            set { signId = value; }
        }

        public SignAnother()
        {
            InitializeComponent();

            ExceptionInformationSign.Text = "";
        }

        //当前使用者信息
        public UserRole userRole;
        public UserRole UserRoler
        {
            get { return userRole; }
            set { userRole = value; }
        }

        public SignAnother(String signer, String projectId, String subProjectId)
        {
            InitializeComponent();
            UserRoler = new UserRole();
            UserRoler.ProjectId = projectId;
            UserRoler.SubProjectId = subProjectId;
            UserRoler.UserId = signer;
            authorizeDao = new AuthorizeDao();
            userRoleDao = new UserRoleDao();
            accountDao = new AccountDao();
            userRoleNoCodeList = null;
             userRoleCodeList = new List<string>();
            //getCommissionListUseCode();
            setCommissionList(userRoleCodeList);
            ExceptionInformationSign.Text = "";
        }

        //获得不用授权码时的可选对象
        public void getCommissionListNoCode()
        {
            List<String> mayorIdList = authorizeDao.getMajorIDByUser(UserRoler.UserId);
            userRoleNoCodeList = new List<String>();
            for (int i = 0; i < mayorIdList.Count; i++)
            {
                UserRole tempUserRole = userRoleDao.getRoleInfoByMajorID(mayorIdList[i]);
                if (tempUserRole != null && UserRoler.ProjectId.Equals(tempUserRole.ProjectId) && UserRoler.SubProjectId.Equals(tempUserRole.SubProjectId))
                {
                    userRoleNoCodeList.Add(mayorIdList[i]);
                }
            }
        }

        //         //获得用授权码时的可选对象
        //         public void getCommissionListUseCode()
        //         {
        //             userRoleCodeList = userRoleDao.getRoleInfoByProId(UserRoler.ProjectId, UserRoler.SubProjectId);
        //         }

        //设置下拉菜单
        public void setCommissionList(List<String> userRoleList)
        {
            for (int i = 0; i < userRoleList.Count; i++)
            {
                ComboBoxItem cbi1 = new ComboBoxItem();
                cbi1.Text = userRoleList[i];
                cbi1.Value = userRoleList[i];
                CommissionName.Items.Add(cbi1);
            }

            ComboBoxItem cbi= new ComboBoxItem();
            cbi.Text = "陈志东";
            cbi.Value = "12";
            CommissionName.Items.Add(cbi);

        }
        public String validateUser()
        {
            if (UserRoler != null)
            {
                if (UserRoler.UserId == null || UserRoler.SubProjectId == null || UserRoler.ProjectId == null)
                {
                    return "用户名或项目号、子项目号未知";
                }
                else
                {
                    return "success";
                }
            }
            else
            {

                return "未获得用户信息";
            }
        }

        private void CommissionCodeNotRequired_CheckedChanged(object sender, EventArgs e)
        {
            if (true == CommissionCodeNotRequired.Checked)
            {
                if (userRoleNoCodeList == null)
                {
                    getCommissionListNoCode();
                }
                CommissionPwd.Enabled = false;
                CommissionName.Items.Clear();
                setCommissionList(userRoleNoCodeList);
            }
            else
            {
                //                 if (userRoleCodeList == null)
                //                 {
                //                     getCommissionListUseCode();
                //                 }
                CommissionName.Items.Clear();
                //setCommissionList(userRoleCodeList);
                CommissionPwd.Enabled = true;
            }

        }

        public void getCommissionListSelect()
        {
            if (CommissionName.SelectedItem == null)
            {
                if (CommissionName.Text.Equals(""))
                {
                    SignId = null;
                }
                else
                {
                    SignId = CommissionName.Text;
                }
            }
            else
            {
                SignId = ((ComboBoxItem)CommissionName.SelectedItem).Value.ToString();
            }
        }
        //判断授权码是否正确
        private bool isAuthUseCode()
        {
            getCommissionListSelect();
            if (SignId == null)
            {
                return false;
            }

            //Account accout = accountDao.getAccountById(SignId);
            //if (CommissionPwd.Text.Equals(accout.AuthPassword))
            if (CommissionPwd.Text.Equals("123"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //判断授权无授权码时是否正确
        private bool isAuthUseNoCodeOk()
        {
            getCommissionListSelect();
            if (SignId == null)
            {
                return false;
            }
            return authorizeDao.isAuthNoCode(UserRoler.UserId, SignId);
        }

        private void CommissionConfirm_Click(object sender, EventArgs e)
        {
            SignControl signControl = (SignControl)this.Owner;
            if (false == CommissionCodeNotRequired.Checked)
            {
                if (isAuthUseCode())
                {
                    Account account = new Account();
                    account.Signfile = "C:\\TJADRICD\\Sign\\陈志东.vrf";
                    signControl.signControlLoad(account.Signfile);
                    //signControl.signControlLoad(accountDao.getAccountById(SignId).Signfile);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("授权码错误");
                }
            }
            else
            {
                if (isAuthUseNoCodeOk())
                {
                    Account account = new Account();
                    account.Signfile = "C:\\TJADRICD\\Sign\\陈志东.vrf";
                    signControl.signControlLoad(account.Signfile);
                    //signControl.signControlLoad(accountDao.getAccountById(SignId).Signfile);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("未得到授权");
                }
            }
        }

        private void ExceptionInformationSign_Click(object sender, EventArgs e)
        {

        }

        private void SignAnother_Load(object sender, EventArgs e)
        {

        }
    }
}
